﻿using SD.Infrastructure.Repository.EntityFrameworkCore;
using SD.Infrastructure.RepositoryBase;
using SlamDunk.MES.Domain.Entities.PlanContxt;
using SlamDunk.MES.Domain.IRepositories.Interfaces.PlanContxt;
using SlamDunk.ValueObjects.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace SlamDunk.MES.Repository.Implements.PlanContxt
{
    /// <summary>
    /// 作业计划仓储实现
    /// </summary>
    public class ExecutionPlanRepository : EFAggRootRepositoryProvider<ExecutionPlan>, IExecutionPlanRepository
    {
        #region # 分页获取作业计划列表 —— ICollection<ExecutionPlan> FindByPage(string keywords...
        /// <summary>
        /// 分页获取作业计划列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="productId">产品Id</param>
        /// <param name="processId">工艺Id</param>
        /// <param name="bomId">物料清单Id</param>
        /// <param name="productionPlanId">生产计划Id</param>
        /// <param name="status">作业计划状态</param>
        /// <param name="checkStatus">审核状态</param>
        /// <param name="hasGeneratedWorkOrders">是否已生成工单</param>
        /// <param name="hasInspected">是否已质检</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="facilityIds">工厂模型Id集</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="rowCount">总记录数</param>
        /// <param name="pageCount">总页数</param>
        /// <returns>作业计划列表</returns>
        public ICollection<ExecutionPlan> FindByPage(string keywords, Guid? productId, Guid? processId, Guid? bomId, Guid? productionPlanId, ExecutionPlanStatus? status, CheckStatus? checkStatus, bool? hasGeneratedWorkOrders, bool? hasInspected, DateTime? startTime, DateTime? endTime, IList<Guid> facilityIds, int pageIndex, int pageSize, out int rowCount, out int pageCount)
        {
            Expression<Func<ExecutionPlan, bool>> condition =
                x =>
                    (string.IsNullOrEmpty(keywords) || x.Keywords.Contains(keywords)) &&
                    (productId == null || x.ProductId == productId) &&
                    (processId == null || x.ProcessId == processId) &&
                    (bomId == null || x.BomId == bomId) &&
                    (productionPlanId == null || x.ProductionPlanId == productionPlanId) &&
                    (status == null || x.Status == status) &&
                    (checkStatus == null || x.CheckStatus == checkStatus) &&
                    (hasGeneratedWorkOrders == null || x.HasGeneratedWorkOrders == hasGeneratedWorkOrders) &&
                    (hasInspected == null || x.HasInspected == hasInspected) &&
                    (startTime == null || x.ExpStartTime >= startTime) &&
                    (endTime == null || x.ExpEndTime <= endTime);

            IQueryable<ExecutionPlan> productionPlans = base.Find(condition);
            if (facilityIds != null && facilityIds.Any())
            {
                productionPlans = productionPlans.Where(x => x.FacilityId.HasValue && facilityIds.Contains(x.FacilityId.Value));
            }

            IOrderedQueryable<ExecutionPlan> orderedResult = productionPlans.OrderByDescending(x => x.AddedTime);
            IQueryable<ExecutionPlan> pagedResult = orderedResult.ToPage(pageIndex, pageSize, out rowCount, out pageCount);

            return pagedResult.ToList();
        }
        #endregion

        #region # 获取作业计划状态/数量字典 —— IDictionary<ExecutionPlanStatus, int> GetExecutionPlanStatusCounts(...
        /// <summary>
        /// 获取作业计划状态/数量字典
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>作业计划状态/数量字典</returns>
        public IDictionary<ExecutionPlanStatus, int> GetExecutionPlanStatusCounts(DateTime startTime, DateTime endTime)
        {
            Expression<Func<ExecutionPlan, bool>> condition =
                x =>
                    x.ExpStartTime >= startTime &&
                    x.ExpEndTime <= endTime;

            IQueryable<ExecutionPlan> executionPlans = base.Find(condition);
            var executionPlanGroups = executionPlans
                    .GroupBy(x => x.Status)
                    .Select(x => new
                    {
                        Key = x.Key,
                        Value = x.Count()
                    });

            IDictionary<ExecutionPlanStatus, int> dictionary = executionPlanGroups.ToDictionary(x => x.Key, x => x.Value);

            return dictionary;
        }
        #endregion
    }
}
